package com.yjx.controller;

import com.yjx.dto.ReceptionistVO;
import com.yjx.dto.RepairQueryModule;
import com.yjx.pojo.Repair;
import com.yjx.service.RepairService;
import com.yjx.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController//@Restcontroller注解相当于@Controller和@ResponseBody的组合
@RequestMapping("/repair")
public class RepairController {
    @Autowired
    private RepairService repairService;

    @GetMapping("/getAllRepair")
    public Result<Map<String, Object>> getAllRepair(@ModelAttribute RepairQueryModule repairQueryModule) {
        return repairService.getRepairListByCondition(repairQueryModule);
    }

    @GetMapping("/getAllReceptionist")
    public Result<List<ReceptionistVO>> getAllReceptionist() {
        // 1. 调用Service获取接待人员列表
        List<ReceptionistVO> receptionists = repairService.getAllReceptionist();
        // 2. 返回成功响应（code=200，data=接待人员列表）
        return Result.success(receptionists);
    }

    @PostMapping("/deleteRepair")
    public Result<Object> deleteRepair(
            @RequestParam("repairId") Integer repairId,
            @RequestParam("userId") Integer userId,
            @RequestParam("password") String password) {

        // 调用Service删除
        boolean isDeleted = repairService.deleteRepair(repairId, userId, password);
        if (isDeleted) {
            return Result.success(null); // 成功响应（code=200）
        } else {
            return Result.fail("订单不存在或无删除权限", 403); // 无权限/无数据（code=403）
        }
    }

    @PostMapping("/createRepair")
    public Result<Object> createRepair(@RequestBody Repair repair) {
        // 调用Service层创建订单
        boolean isCreated = repairService.createRepair(repair);
        if (isCreated) {
            return Result.success("订单创建成功");
        } else {
            return Result.fail("订单创建失败", 500);
        }
    }
}
